<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryParams.keyword"
          style="margin-bottom:10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="changeInput"
        />
        <!-- 树形组件 -->
        <el-tree
          ref="deptTree"
          node-key="id"
          highlight-current
          :expand-on-click-node="false"
          default-expand-all
          :data="depts"
          :props="defaultProps"
          @current-change="currentChange"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button v-permission="'add-employee'" size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog=true">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" label="头像">
            <template #default="{row}">
              <el-avatar v-if="row.staffPhoto" class="username" :src="row.staffPhoto" />
              <span v-else class="username">{{ row.username }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template #default="{row}">
              <span v-if="row.formOfEmployment===1">正式</span>
              <span v-else-if="row.formOfEmployment===2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" size="mini" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button type="text" size="mini" @click="btnRole(row.id)">角色</el-button>
              <el-button type="text" size="mini">删除</el-button>
            </template>
            <template>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onconfirm="onconfirm"
              >
                <!-- <el-button slot="reference">删除</el-button> -->
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="end" style="height:60px" align="middle">
          <el-pagination
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            layout="total,prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </div>
    </div>
    <ImportExcel :show-excel-dialog.sync="showExcelDialog" @updataSuccess="getEmoloyeeList" />
    <!--
      el-checkbox-group  v-model 选中/勾选的角色列表[]
       el-checkbox 多个复选框 通过 label 可以收集选中的角色
      -->
    <el-dialog
      title="分配角色"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <el-checkbox-group v-model="roleid">
        <el-checkbox v-for="item in rolelist" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="btnRoleok">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import ImportExcel from './components/imprt-excel.vue'
import { getEnableRoleList, assignRoles } from '@/api/role'
import { getDpartment } from '@/api/department'
import { translateListToTree } from '@/utils'
import { getEmoloyeeList, exportEmployee, getEmoloyeeDetail } from '@/api/employee'
import FileSaver from 'file-saver'
import ImportExcel from './components/import-excel.vue'

export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      roleid: [], // 选中的角色列表
      rolelist: [], // 角色列表
      currenRoleId: '',
      list: [],
      depts: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      queryParams: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: ''// 关键字
      },
      total: 0,
      showExcelDialog: false, //
      dialogVisible: false
    }
  },
  created() {
    this.getDpartment()
  },
  methods: {
    // 点击角色弹出框
    async btnRole(id) {
      this.currenRoleId = id
      // 1.角色列表赋值
      this.rolelist = await getEnableRoleList()
      // 2.显示该用户拥有的角色信息
      const { roleIds } = await getEmoloyeeDetail(id)
      // console.log(res)
      // 3.回显角色选中信息
      this.roleid = roleIds
      // 4.显示弹出层-先渲染-再展示弹层
      this.dialogVisible = true

      // console.log(res)
    },
    async btnRoleok() {
      console.log(66)
      await assignRoles({ id: this.currenRoleId, roleIds: this.roleid })
      this.dialogVisible = true
    },
    // cxcel导出roleIds
    async exportEmployee() {
      const res = await exportEmployee()
      console.log(res)
      FileSaver.saveAs(res, '员工表.xlsx')
    },
    // 右表数据获取
    async getEmoloyeeList() {
      const { rows, total } = await getEmoloyeeList(this.queryParams)
      console.log(rows)
      this.list = rows
      this.total = total
    },
    // 搜索框
    changeInput() {
      clearTimeout(this.timer)
      this.timer = setTimeout(() => {
        // 页码重置
        this.queryParams.page = 1
        // 查询员工列表
        this.getEmoloyeeList()
      }, 300)
    },
    handleCurrentChange(val) {
      this.queryParams.page = val
      // this.currentChange()
      this.getEmoloyeeList()
    },
    // 左侧树形数据获取
    async getDpartment() {
      // const res = await getDpartment()
      this.depts = translateListToTree(await getDpartment(), 0)
      // console.log(res)
      this.queryParams.departmentId = this.depts[0].id
      // 渲染是异步的-等待机制
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentKey(this.queryParams.departmentId)
      })
      this.getEmoloyeeList()
    },
    // 节点切换
    currentChange(node) {
      // node 当前选中的节点的信息-对象
      this.queryParams.departmentId = node.id
      // 重新设置页码-第一页
      this.queryParams.page = 1
      this.getEmoloyeeList()
    },
    del() {
      console.log(666)
    },
    onconfirm() {
      console.log(999)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
